import express from "express";
import ResponseHelper from "../ResponseHelper";
import KeyChunkService from "../../services/KeyChunkService";
import { keyChunkStatus } from "../../models/enums";

var router = express.Router()

router.get('/getOne', async (req, res, next) => {
  const id = req.query.id || ''

  try {
    const result = await KeyChunkService.getOneUndistributed(id)

    ResponseHelper.send(result, res)

  } catch (error) {
    next()
  }

})

router.get('/findByPage', async (req, res, next) => {
  const page = req.query.page || 1
  const limit = req.query.limit || 10
  const status = req.query.status || keyChunkStatus.undistributed

  try {
    const result = await KeyChunkService.findByPage(+page, +limit, { status: status })
    return ResponseHelper.send(result, res)
  } catch (err) {
    next()
  }

})

// 获取匹配项
router.get('/right', async (req, res, next) => {
  const result = await KeyChunkService.getRight()
  ResponseHelper.send(result, res)
})

router.put('/', async (req, res, next) => {
  const data = req.body || {}
  /*
    id:chunk_id
    status:2,
    result:消耗的时间
  */

  const result = await KeyChunkService.putOneKey(data)

  ResponseHelper.send(result, res)
  // if (result) {
  //   // @ts-ignore
  //   const keyChunkID = result.id
  //   this.putOneKey({
  //     id: keyChunkID,
  //     status: keyChunkStatus.await,
  //     reserve: JSON.stringify({ node_id: id })
  //   })
  // }



})

// 计算结果
router.post('/', async (req, res, next) => {
  const body = req.body || {}


})


export default router
